﻿using System;
using System.Text.RegularExpressions;
using WikiPlex.Contrib.Macros;
using WikiPlex.Contrib.Renderers;
using Xunit;
using Xunit.Extensions;

namespace WikiPlex.Contrib.IntegrationTests
{
    public class FormattingFacts : IDisposable
    {
        private static readonly Regex WhitespaceRemovalRegex = new Regex(@"\r|\n|\t|\s{3,4}", RegexOptions.Compiled);

        public FormattingFacts()
        {
            WikiPlex.Macros.Register<ColoredTextMacro>();
            WikiPlex.Renderers.Register<ColoredTextRenderer>();
        }

        public void Dispose()
        {
            WikiPlex.Macros.Unregister<ColoredTextMacro>();
            WikiPlex.Renderers.Unregister<ColoredTextRenderer>();
        }

        [Theory]
        [InputData("ColoredText")]
        public void WillVerifyFormatting(string inputFile, string expectedFile, string prefix)
        {
            string expectedText = InputDataAttribute.ReadContent(prefix, expectedFile);
            string actualText = new WikiEngine().Render(InputDataAttribute.ReadContent(prefix, inputFile));

            // comment out the following lines if you wish to compare
            // the whitespace correctly
            expectedText = WhitespaceRemovalRegex.Replace(expectedText, string.Empty);
            actualText = WhitespaceRemovalRegex.Replace(actualText, string.Empty);

            Assert.Equal(expectedText, actualText);
        }
    }
}
